package com.example.Utils;

import android.os.StrictMode;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by fany on 14-2-12.
 */
public class HttpHelper {
    private static HttpURLConnection CreateHttpURLConnection(
            String url, String method, String contentType, String encode){
        HttpURLConnection connection = null;
        try {
            URL urlObject;
            urlObject = new URL(url);
            connection = (HttpURLConnection)urlObject.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            if(method.toUpperCase() == "POST"){
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-type",contentType);
                connection.setRequestProperty("Accept-Charset", encode);
                connection.setRequestProperty("contentType", encode);
                connection.setInstanceFollowRedirects(true);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return connection;
    }

    private static void  setNetworkPolicy(){
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
                .detectLeakedClosableObjects()
                .penaltyLog()
                .penaltyDeath()
                .build());
    }
    public static String GET(String url, String contentType, String encode, String param)  {
        String resultData = "";
        try {
//            setNetworkPolicy();
            HttpGet getMethod = new HttpGet(url + "?" + param);
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(getMethod);
            resultData = EntityUtils.toString(response.getEntity());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultData;
    }

    public static String POST(String url, String contentType, String content, String encode)  {
        String resultData = "";
        try {
            HttpURLConnection connection = CreateHttpURLConnection(url, "POST",contentType,encode);
            connection.setRequestProperty("Content-Type",contentType);
            connection.connect();
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(content);
            outputStream.flush();
            outputStream.close();
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultData;
    }

    //转换为%E4%BD%A0形式
    public static String TextToURL(String s, String encoding)
    {
        String result = "";
        try {
            result=URLEncoder.encode(s, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    //将%E4%BD%A0转换为汉字
    public static String URLToText(String s, String encoding)
    {
        String result = "";
        try {
            result =URLDecoder.decode(s, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
}
